package com.demo.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.demo.client.OneClient;
import com.demo.domain.Car;

import lombok.extern.slf4j.Slf4j;

@RestController
@Slf4j
public class RestControllerTest {

	/* ribbon RestTemplate */
	// @Autowired
	// private RestTemplate restTemplate;

	/* 负载均衡ribbon RestTemplate */
	@Autowired
	@LoadBalanced
	private RestTemplate restTemplateBalance;

	/* feign接口 */
	@Autowired
	private OneClient oneClient;

	/* 发现consul服务 */
	@Autowired
	private DiscoveryClient discoveryClient;

	@RequestMapping("/feign")
	public Map<String, Object> feign(@RequestParam(value = "name") String name) {
		Map<String, Object> map = new HashMap<String, Object>();
		System.out.println("对 spingboot_cloud_client_one_second 发起 feign 请求:" + name);
		String rs = oneClient.getFromTwo(name);
		System.out.println("通过 feign 返回数据:" + rs);
		List<ServiceInstance> ls = serviceUrl();
		map.put("result", rs);
		map.put("client", ls);
		return map;
	}

	@RequestMapping("/ribbon")
	public String ribbon() {
		System.out.println("对 spingboot_cloud_client_one_second 发起 ribbon 请求!");
		String rs = restTemplateBalance.getForObject("http://service-two/hi?name=这个是参数！", String.class);
		System.out.println("通过 ribbon 返回数据:" + rs);
		return "hi ,i'm Pakho , query ribbon result: " + rs;
	}

	@RequestMapping("/ribboncar")
	public String ribbonCar() {
		System.out.println("对 spingboot_cloud_client_one_second 发起 ribbon 请求!");
		Car c = restTemplateBalance.getForObject("http://service-two/car?name=这个是参数！", Car.class);
		System.out.println("通过 ribbon 返回数据:" + c);
		return "hi ,i'm Pakho , query ribbon result: " + c.toString();
	}

	@RequestMapping("/ribbonbalance")
	public String ribbonBalance() {
		System.out.println("对 spingboot_cloud_client_one_second 发起 ribbon 请求!");
		String rs = restTemplateBalance.getForObject("http://service-two/hi?name=这个是参数！", String.class);
		System.out.println("通过 ribbon 负载均衡  返回数据:" + rs);
		return "hi ,i'm Pakho , query ribbon result: " + rs;
	}

	public List<ServiceInstance> serviceUrl() {
		List<ServiceInstance> list = discoveryClient.getInstances("service-two");
		// for (ServiceInstance serviceInstance : list) {
		// System.err.println(serviceInstance.getUri());
		// }
		List<String> services = discoveryClient.getServices();
		for (String string : services) {
			System.err.println("services:" + string);
		}
		return list;
	}
}
